﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.Odbc;
using System.Runtime.InteropServices; 

namespace Watcher
{
    public partial class Form1 : Form
    {

        public enum MessageType {
            WHISPER_MESSAGE,
            SAY_MESSAGE,
            YELL_MESSAGE,
            GUILD_MESSAGE,
            BATTLEGROUND_MESSAGE,
            PARTY_MESSAGE
        };

        public struct FLASHWINFO
        {
            public Int32 cbSize;
            public IntPtr hwnd;
            public Int32 dwFlags;
            public Int32 uCount;
            public Int32 twTimeout;
        }

        [DllImport("user32")]
        private static extern int FlashWindowEx(ref FLASHWINFO pwfi);

        public enum FLASHWINFOFLAGS
        {
            FLASHW_STOP = 0,
            FLASHW_CAPTION = 0x00000001,
            FLASHW_TRAY = 0x00000002,
            FLASHW_ALL = (FLASHW_CAPTION | FLASHW_TRAY),
        }

       

        private string ConStr = "DSN=WatchMyToon_DB";
        private OdbcConnection connection;
        Timer t1;

        bool blinking = false;
        NotifyIcon notico;
        ContextMenu cm;
        public Form1()
        {

            InitializeComponent();
            this.button_dis.KeyDown += new KeyEventHandler(button_dis_KeyDown);
            this.SizeChanged += new EventHandler(Form1_SizeChanged);
            cm = new ContextMenu();
            notico = new NotifyIcon();
            notico.Icon = new Icon(Application.StartupPath + "\\Images\\ic.ico"); // Eigenes Icon einsetzen
            notico.Text = "Show WatchMyToon";   // Eigenen Text einsetzen
            //notico.Visible = true;
            notico.ContextMenu = cm;
            notico.DoubleClick += new EventHandler(notico_DoubleClick);
            notico.BalloonTipText = "WatchMyToon has went to Tray";
        }

        void notico_DoubleClick(object sender, EventArgs e)
        {
            this.Show();
            this.Focus();
            notico.Visible = false;
        }

        void Form1_SizeChanged(object sender, EventArgs e)
        {
            if (this.WindowState == FormWindowState.Minimized)
            {
                notico.Visible = true;
                this.Hide();
                notico.ShowBalloonTip(1000);
            }
        }

        void button_dis_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.F10)
            {
                ClearDB db = new ClearDB();
                db.ShowDialog();
            }
        }

        public void SendMessage(MessageType type, string Message, int toonid, string to = null)
        {
            
            if (connection.State == ConnectionState.Open)
            {
            
                lock (connection)
                {
                    OdbcCommand command = connection.CreateCommand();
                    switch (type)
                    {
                        case MessageType.GUILD_MESSAGE:

                            command.CommandText = "INSERT INTO `TABMessageToSend` (`nToonId`, `szMessageType`, `szMessagePartner`, `szMessageText`, `dtTime`) VALUES (" + toonid + ",'GUILD_MESSAGE', 'GUILD','" + Message + "', CURRENT_TIMESTAMP);";
                            command.ExecuteNonQuery();
                            break;

                        case MessageType.SAY_MESSAGE:

                            command.CommandText = "INSERT INTO `TABMessageToSend` (`nToonId`, `szMessageType`, `szMessagePartner`, `szMessageText`, `dtTime`) VALUES (" + toonid + ",'SAY_MESSAGE', 'SAY','" + Message + "', CURRENT_TIMESTAMP);";
                            command.ExecuteNonQuery();
                            break;

                        case MessageType.WHISPER_MESSAGE:

                            command.CommandText = "INSERT INTO `TABMessageToSend` (`nToonId`, `szMessageType`, `szMessagePartner`, `szMessageText`, `dtTime`) VALUES (" + toonid + ",'WHISPER_MESSAGE','" + to + "','" + Message + "', CURRENT_TIMESTAMP);";
                            command.ExecuteNonQuery();
                            break;

                        case MessageType.YELL_MESSAGE:

                            command.CommandText = "INSERT INTO `TABMessageToSend` (`nToonId`, `szMessageType`, `szMessagePartner`, `szMessageText`, `dtTime`) VALUES (" + toonid + ",'YELL_MESSAGE', 'YELL','" + Message + "', CURRENT_TIMESTAMP);";
                            command.ExecuteNonQuery();
                            break;

                        case MessageType.BATTLEGROUND_MESSAGE:

                            command.CommandText = "INSERT INTO `TABMessageToSend` (`nToonId`, `szMessageType`, `szMessagePartner`, `szMessageText`, `dtTime`) VALUES (" + toonid + ",'BATTLEGROUND_MESSAGE', 'BG','" + Message + "', CURRENT_TIMESTAMP);";
                            command.ExecuteNonQuery();
                            break;

                        case MessageType.PARTY_MESSAGE:

                            command.CommandText = "INSERT INTO `TABMessageToSend` (`nToonId`, `szMessageType`, `szMessagePartner`, `szMessageText`, `dtTime`) VALUES (" + toonid + ",'PARTY_MESSAGE', 'PARTY','" + Message + "', CURRENT_TIMESTAMP);";
                            command.ExecuteNonQuery();
                            break;

                        default :
                            break;

                    }
                }
            }
            
        }

        public void UpdateChats()
        {

        }


        private string NamenErzeugen(string Name)
        {
            System.Text.StringBuilder sb = new System.Text.StringBuilder();
            foreach (string s in Name.Split(' '))
                if (s.Length > 0)
                    sb.Append(s.Substring(0, 1).ToUpper()).Append(s.Substring(1)).Append(" ");
            return sb.ToString();
        } 

        // Diese funktion wird immer wieder aufgerufen
        public void Update(object cl)
        {
            ClassTab _cl = (ClassTab)cl;

            if (connection.State == ConnectionState.Open)
            {

                lock (connection)
                {

                    OdbcCommand command = connection.CreateCommand();
                    OdbcDataReader reader;

                    command.CommandText = "SELECT * FROM `TABToonsDetail` WHERE nToonId = " + _cl.CharID + ";";
                    reader = command.ExecuteReader();

                    if (reader.HasRows)
                    {
                        while (reader.Read())
                        {

                            if (reader["bIsInGuild"].ToString().Equals("-1"))
                            {
                                _cl.CreateChatTab("Guild", "GUILD", true);
                                _cl.HandleGuildMenu(true);
                            }
                            else
                            {
                                _cl.RemoveChatTab("Guild");
                                _cl.HandleGuildMenu(false);
                            }

                            if (reader["bIsInParty"].ToString().Equals("-1"))
                            {
                                _cl.CreateChatTab("Party", "PARTY", true);
                                _cl.HandleGroupMenu(true);
                            }
                            else
                            {
                                _cl.RemoveChatTab("Party");
                                _cl.HandleGroupMenu(false);
                            }

                        }

                    }

                    reader.Close();
                    command.CommandText = "Select * from `TABMessageReceive` WHERE nToonId = " + _cl.CharID + ";";

                    reader  = command.ExecuteReader();

                    if (reader.HasRows)
                    {
                      
                        while (reader.Read())
                        {
                            Boolean bdelete = false;
                            int nMessageID = Convert.ToInt32(reader["nMsgId"]);

                            string szMessageType = reader["szMessageType"].ToString();
                            string szMessagePartner = reader["szMessagePartner"].ToString();
                            string szMessageText = reader["szMessageText"].ToString();

                            switch (szMessageType)
                            {
                                case "GUILD_MESSAGE":
                                    lock (_cl.Controls["Chats"].Controls["Guild"].Controls["text_chat"])
                                    {
                                        _cl.Controls["Chats"].Controls["Guild"].Controls["text_chat"].Text += "[" + szMessagePartner + "]: " + szMessageText + "\r\n";
                                    }

                                    if (((TabControl)_cl.Controls["Chats"]).SelectedTab.Name != "Guild")
                                    {
                                        ((TabPage)_cl.Controls["Chats"].Controls["Guild"]).ImageIndex = 1;
                                    }
                                    bdelete = true;
                                    break;
                                case "PARTY_MESSAGE":
                                    lock (_cl.Controls["Chats"].Controls["Party"].Controls["text_chat"])
                                    {
                                        _cl.Controls["Chats"].Controls["Party"].Controls["text_chat"].Text += "[" + szMessagePartner + "]: " + szMessageText + "\r\n";
                                    }

                                    if (((TabControl)_cl.Controls["Chats"]).SelectedTab.Name != "Party")
                                    {
                                        ((TabPage)_cl.Controls["Chats"].Controls["Party"]).ImageIndex = 1;
                                    }
                                    bdelete = true;
                                    break;
                                case "SAY_MESSAGE":
                                    lock (_cl.Controls["Chats"].Controls["Say"].Controls["text_chat"])
                                    {
                                        _cl.Controls["Chats"].Controls["Say"].Controls["text_chat"].Text += "[" + szMessagePartner + "]: " + ": " + szMessageText + "\r\n";
                                    }

                                    if (((TabControl)_cl.Controls["Chats"]).SelectedTab.Name != "Say")
                                    {
                                        ((TabPage)_cl.Controls["Chats"].Controls["Say"]).ImageIndex = 1;
                                    }
                                     bdelete = true;
                                    break;
                                case "WHISPER_MESSAGE":
                                    bool bfound = false;
                                    foreach (TabPage Tpage in ((TabControl)_cl.Controls["Chats"]).TabPages)
                                    {
                                        if (Tpage.Name == szMessagePartner)
                                        {
                                            lock (_cl.Controls["Chats"].Controls[szMessagePartner].Controls["text_chat"])
                                            {
                                                _cl.Controls["Chats"].Controls[szMessagePartner].Controls["text_chat"].Text += "[" + szMessagePartner + "]: " + szMessageText + "\r\n";
                                            }

                                            if (((TabControl)_cl.Controls["Chats"]).SelectedTab.Name != szMessagePartner)
                                            {
                                                ((TabPage)_cl.Controls["Chats"].Controls[szMessagePartner]).ImageIndex = 1;
                                            }

                                            bfound = true;
                                        }
                                    }
                                    if (!bfound)
                                    {
                                        _cl.CreateChatTab(szMessagePartner, "WHISPER");
                                        lock (_cl.Controls["Chats"].Controls[szMessagePartner].Controls["text_chat"])
                                        {
                                            _cl.Controls["Chats"].Controls[szMessagePartner].Controls["text_chat"].Text += "[" + szMessagePartner + "]: " + szMessageText + "\r\n";

                                        }

                                        ((TabPage)_cl.Controls["Chats"].Controls[szMessagePartner]).ImageIndex = 1;

                                    }

                                    bfound = false;
                                    bdelete = true;
                                    break;
                                case "BATTLEGROUND_MESSAGE":
                                    bfound = false;
                                    foreach (TabPage Tpage in ((TabControl)_cl.Controls["Chats"]).TabPages)
                                    {
                                        if (Tpage.Name == "Battleground")
                                        {
                                            lock (_cl.Controls["Chats"].Controls["Battleground"].Controls["text_chat"])
                                            {
                                                _cl.Controls["Chats"].Controls["Battleground"].Controls["text_chat"].Text += "[" + szMessagePartner + "]: " + szMessageText + "\r\n";
                                            }

                                            if (((TabControl)_cl.Controls["Chats"]).SelectedTab.Name != "Battleground")
                                            {
                                                ((TabPage)_cl.Controls["Chats"].Controls["Battleground"]).ImageIndex = 1;
                                            }

                                            bfound = true;
                                        }
                                    }
                                    if (!bfound)
                                    {
                                        _cl.CreateChatTab("Battleground", "BG");
                                        lock (_cl.Controls["Chats"].Controls["Battleground"].Controls["text_chat"])
                                        {
                                            _cl.Controls["Chats"].Controls["Battleground"].Controls["text_chat"].Text += "[" + szMessagePartner + "]: " + szMessageText + "\r\n"; 

                                        }

                                        ((TabPage)_cl.Controls["Chats"].Controls["Battleground"]).ImageIndex = 1;

                                    }

                                    bfound = false;
                                    bdelete = true;
                                    break;
                                case "YELL_MESSAGE":
                                    lock (_cl.Controls["Chats"].Controls["Yell"].Controls["text_chat"])
                                    {
                                        _cl.Controls["Chats"].Controls["Yell"].Controls["text_chat"].Text += "[" + szMessagePartner + "]: " + ": " + szMessageText + "\r\n";
                                    }

                                    if (((TabControl)_cl.Controls["Chats"]).SelectedTab.Name != "Yell")
                                    {
                                        ((TabPage)_cl.Controls["Chats"].Controls["Yell"]).ImageIndex = 1;
                                    }
                                    break;
                            }

                            if(bdelete)
                            {
                                OdbcCommand delete = connection.CreateCommand();
                                delete.CommandText = "DELETE FROM `TABMessageReceive` WHERE nToonId = " + _cl.CharID + " AND nMsgId = " + nMessageID + ";";
                                delete.ExecuteNonQuery();
                            }


                        }

                        if (tabControl1.SelectedTab.Name != _cl.Name)
                        {
                            tabControl1.TabPages[_cl.Name].ImageIndex = 1;
                        }

                        if (!this.TopMost && blinking == false)
                        {
                            blinking = true;
                            Flash();
                        }
                    }

                    reader.Close();


                    command.CommandText = "Select * from `TABToonsDetail` WHERE nToonId = " + _cl.CharID + ";";

                    OdbcDataReader read = command.ExecuteReader();

                    while (read.Read())
                    {
                       
                       
                        _cl.Controls["Lvl"].Text = "Level: " + read["nCurrentLvl"].ToString();
                        ((ProgressBar)_cl.Controls["XpBar"]).Maximum = Convert.ToInt32(read["nNextLvlXp"].ToString());
                        ((ProgressBar)_cl.Controls["XpBar"]).Minimum = 0;
                        ((ProgressBar)_cl.Controls["XpBar"]).Value = Convert.ToInt32(read["nCurrentXp"].ToString());
                        int percent = (int)(((double)((ProgressBar)_cl.Controls["XpBar"]).Value / (double)((ProgressBar)_cl.Controls["XpBar"]).Maximum) * 100);
                        ((ProgressBar)_cl.Controls["XpBar"]).CreateGraphics().DrawString(percent.ToString() + "%", new Font("Arial", (float)8.25, FontStyle.Regular), Brushes.Black, new PointF(((ProgressBar)_cl.Controls["XpBar"]).Width / 2 - 10, ((ProgressBar)_cl.Controls["XpBar"]).Height / 2 - 7));


                        _cl.SetCurrentZone(read["szZoneText"].ToString() + " - " + read["szSubZone"].ToString());
                        _cl.SetBagStat(read["nFreeBagSlot"].ToString() + " / " + read["nTotalBagSlot"].ToString());



                        _cl.SetCurrentCoords(Convert.ToInt32(read["szZoneId"].ToString()), Convert.ToInt32(read["nZoneLevelNr"].ToString()), Convert.ToInt32(read["nXposition"].ToString()), Convert.ToInt32(read["nYposition"].ToString()));

                    }
                    read.Close();

                    command.CommandText = "Select * from `TABLoots` WHERE nToonId = " + _cl.CharID + ";";
                    read = command.ExecuteReader();
                    while (read.Read())
                    {
                        StringBuilder sb = new StringBuilder();
                        string xCol = read["szColor"].ToString();
                        Color c = System.Drawing.ColorTranslator.FromHtml("#" + xCol.ToString());

                        sb.Clear();
                        sb.Append("You loot: [");

                        string name = read["szName"].ToString();
                        sb.Append(name);
                        sb.Append("] x");
                        sb.Append(read["szAmount"].ToString());



                        int nLootNr = Convert.ToInt32(read["nLootNr"]);

                        lock (_cl.Controls["Chats"].Controls["Loots"].Controls["text_looting"])
                        {
                             _cl.Controls["Chats"].Controls["Loots"].Controls["text_looting"].Text += sb.ToString() + "\r\n";
                            ((RichTextBox)_cl.Controls["Chats"].Controls["Loots"].Controls["text_looting"]).SelectedText = c.Name;
                            ((RichTextBox)_cl.Controls["Chats"].Controls["Loots"].Controls["text_looting"]).Select(10, name.Length);
                           
                        }

                        OdbcCommand delete = connection.CreateCommand();
                        delete.CommandText = "DELETE FROM `TABLoots` WHERE nToonId = " + _cl.CharID + " AND nLootNr = " + nLootNr + ";";
                        delete.ExecuteNonQuery();
                    }

                    read.Close();


                    command.CommandText = "Select * from `TABToons` WHERE nToonId = " + _cl.CharID + ";";
                    read = command.ExecuteReader();
                    while (read.Read())
                    {
                        //szCurrentSpec
                        string classinfo = NamenErzeugen(read["szClass"].ToString().ToLower());
                        string classspec = NamenErzeugen(read["szCurrentSpec"].ToString().ToLower());
                        _cl.Controls["classinfo"].Text = classspec + " " +  classinfo;
                       
                    }

                    read.Close();


                    command.CommandText = "Select * from `TABGroupInvites` WHERE nToonId = " + _cl.CharID + ";";
                    read = command.ExecuteReader();
                    while (read.Read())
                    {
                       if(read["szStatus"].Equals("REQUEST_GROUP"))
                       {
                           _cl.AskForGroupInvite(read["szInviterName"].ToString());
                       }

                    }

                    read.Close();
                    read.Dispose();
                    command.Dispose();

                    GC.Collect();

                }
            }

        }

      
        private void Add_char_Click(object sender, EventArgs e)
        {
            if (connection.State == ConnectionState.Open)
            {

                lock (connection)
                {
                    OdbcCommand command = connection.CreateCommand();

                    command.CommandText = "SELECT * FROM `TABVersion`";
                    OdbcDataReader reader = command.ExecuteReader();

                    while (reader.Read())
                    {
                        String szVersionName = reader["szVersionName"].ToString();
                        string[] currenta = szVersionName.Remove(0,1).Split('.');

                        Version current = new Version(Convert.ToInt32(currenta[0]), Convert.ToInt32(currenta[1]), Convert.ToInt32(currenta[2]));
                        if (current < new Version(1,3,2))
                        {
                            MessageBox.Show("Please enable the plugin first to update the DB!!", "Wrong Version");
                            this.Close();
                        }
                    }

                    reader.Close();

                    command.CommandText = "SELECT * FROM `TABToons` WHERE `dtLastUpdate` >= date_sub( CURRENT_TIMESTAMP , INTERVAL 45 SECOND )";

                    reader = command.ExecuteReader();

                    while (reader.Read())
                    {

                        for (int i = 0; i < tabControl1.TabCount; i++)
                        {
                            if (tabControl1.TabPages[i].Name == reader["szName"].ToString())
                            {
                                return;
                            }
                        }

                        string charinfo = reader["szGender"].ToString() + " - " + reader["szRace"].ToString();
                        ClassTab tab = new ClassTab(reader["szName"].ToString(), Convert.ToInt32(reader["nToonId"].ToString()), reader["szFaction"].ToString(), reader["szRealm"].ToString(), charinfo);

                        if (tabControl1.TabCount == 0)
                        {
                            tabControl1.TabPages.Add(tab);
                            tabControl1.Visible = true;
                            txt_no_toons.Visible = false;
                        }else
                        {
                            tabControl1.TabPages.Add(tab);
                        }

                        Update(tab);
                    }

                    reader.Close();

                }

            }
            
 
        }

       
        public void AcceptGroupInvite(int charID)
        {
            if (connection.State == ConnectionState.Open)
            {

                lock (connection)
                {
                    OdbcCommand command = connection.CreateCommand();
                    command.CommandText = "UPDATE `TABGroupInvites` SET `szStatus` = 'ACCEPT_GROUP' WHERE nToonId = " + charID + ";";
                    command.ExecuteNonQuery();


                }
            }
        }

     
        public void DeclineGroupInvite(int charID)
        {
            if (connection.State == ConnectionState.Open)
            {

                lock (connection)
                {
                    OdbcCommand command = connection.CreateCommand();
                    command.CommandText = "UPDATE `TABGroupInvites` SET `szStatus` = 'DECLINE_GROUP' WHERE nToonId = " + charID + ";";
                    command.ExecuteNonQuery();


                }
            }
        }

        public void AddGroupInvite(int charID, string charname)
        {
            if (connection.State == ConnectionState.Open)
            {

                lock (connection)
                {
                    OdbcCommand command = connection.CreateCommand();
                    command.CommandText = "INSERT INTO `TABEventHandling` (`nToonId`, `szEventDesc`, `szEventText`) VALUES (" + charID + ", 'INVITE_GROUP', \"" + charname + "\");";
                    command.ExecuteNonQuery();

                  
                }
            }
        }

        public void LeaveGroup(int charID)
        {
            if (connection.State == ConnectionState.Open)
            {

                lock (connection)
                {
                    OdbcCommand command = connection.CreateCommand();
                    command.CommandText = "INSERT INTO `TABEventHandling` (`nToonId`, `szEventDesc`) VALUES (" + charID + ", 'LEAVE_GROUP');";
                    command.ExecuteNonQuery();


                }
            }
        }

        public void AcceptGuildInvite(int charID)
        {
            if (connection.State == ConnectionState.Open)
            {

                lock (connection)
                {
                    OdbcCommand command = connection.CreateCommand();
                    command.CommandText = "INSERT INTO `TABEventHandling` (`nToonId`, `szEventDesc`) VALUES (" + charID + ", 'ACCEPT_GUILD');";
                    command.ExecuteNonQuery();


                }
            }
        }

        public void DeclineGuildInvite(int charID)
        {
            if (connection.State == ConnectionState.Open)
            {

                lock (connection)
                {
                    OdbcCommand command = connection.CreateCommand();
                    command.CommandText = "INSERT INTO `TABEventHandling` (`nToonId`, `szEventDesc`) VALUES (" + charID + ", 'DECLINE_GUILD');";
                    command.ExecuteNonQuery();


                }
            }
        }

        public void LeaveGuild(int charID)
        {
            if (connection.State == ConnectionState.Open)
            {

                lock (connection)
                {
                    OdbcCommand command = connection.CreateCommand();
                    command.CommandText = "INSERT INTO `TABEventHandling` (`nToonId`, `szEventDesc`) VALUES (" + charID + ", 'LEAVE_GUILD');";
                    command.ExecuteNonQuery();


                }
            }
        }

        public void IgnorePlayer(int charID, string charname)
        {
            if (connection.State == ConnectionState.Open)
            {

                lock (connection)
                {
                    OdbcCommand command = connection.CreateCommand();
                    command.CommandText = "INSERT INTO `TABEventHandling` (`nToonId`, `szEventDesc`, `szEventText`) VALUES (" + charID + ", 'IGNORE_PLAYER', \"" + charname + "\");";
                    command.ExecuteNonQuery();


                }
            }
        }

        void tabControl1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (tabControl1.TabCount != 0)
            {
                if (tabControl1.SelectedTab.ImageIndex != 0)
                {
                    tabControl1.SelectedTab.ImageIndex = 0;
                }
            }
        }

       private void button_connect_Click(object sender, EventArgs e)
        {
            try
            {
                connection = new OdbcConnection(ConStr);
                connection.Open();

                label_conn.ForeColor = Color.Green;
                label_conn.Text = "Connected";
                button_connect.Hide();
                button_dis.Show();

                Add_char.Enabled = true;

                //txt_no_toons.Visible = false;

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error");
            }



        }


       private void Form1_Load(object sender, EventArgs e)
       {
           tabControl1.SelectedIndexChanged += new EventHandler(tabControl1_SelectedIndexChanged);
           this.Activated += new EventHandler(Form1_Activated);
         
       }

       void Form1_Activated(object sender, EventArgs e)
       {
           if (blinking)
           {
               blinking = false;
               Flash();
           }
       }

      
       private void button_dis_Click_1(object sender, EventArgs e)
       {
           connection.Close();
           button_dis.Hide();
           button_connect.Show();
           connection.Close();
           connection.Dispose();

           label_conn.ForeColor = Color.Red;
           label_conn.Text = "Not Connected";

           foreach (TabPage ttp in tabControl1.TabPages)
           {
               ttp.Dispose();
               tabControl1.TabPages.Remove(ttp);
           }

           tabControl1.TabPages.Clear();

           Add_char.Enabled = false;
           txt_no_toons.Visible = true;
           tabControl1.Visible = false;

           GC.Collect();
       }

       private void button1_Click(object sender, EventArgs e)
       {
           this.Close();
           GC.Collect();
       }

    

       private void Flash()
       {
           if (checkBox1.Checked == true)
           {
               FLASHWINFO flash = new FLASHWINFO();
               flash.cbSize = System.Runtime.InteropServices.Marshal.SizeOf(flash);
               flash.hwnd = this.Handle;
               flash.dwFlags = blinking ? (Int32)FLASHWINFOFLAGS.FLASHW_ALL : (Int32)FLASHWINFOFLAGS.FLASHW_STOP;
               flash.uCount = 5;
               flash.twTimeout = 1000;
               FlashWindowEx(ref flash);
           }
       }

       private void button2_Click_1(object sender, EventArgs e)
       {
           String tabname = ((TabControl)tabControl1.SelectedTab.Controls["Chats"]).SelectedTab.Name; ;

           if (tabname.Equals("Loots"))
               ((TabControl)tabControl1.SelectedTab.Controls["Chats"]).SelectedTab.Controls["text_looting"].Text = "";
           else
               ((TabControl)tabControl1.SelectedTab.Controls["Chats"]).SelectedTab.Controls["text_chat"].Text = "";


       }

    
    }
}
